﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ZLDC.Common.Estate;

namespace ZLDC.Estate.UM
{
    public partial class TranTmp : _PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                NewMethod();
            }
        }

        private void NewMethod()
        {
            lblCustomer.Text = "0";
            lblHousing.Text = "0";
            ddldelDept.Items.Clear();
            ddldeluser.Items.Clear();
            ddldelDept.DataSource = ZLDC.DAL.Estate.DeptDAL.getGetAllChildrenDept("", "分行");
            ddldelDept.DataValueField = "id";
            ddldelDept.DataTextField = "deptname";
            ddldelDept.DataBind();
           
            ddldelDept.Items.Insert(0, new ListItem("请选择"));
            ddldeluser.Items.Insert(0, new ListItem("请选择"));

            ddlDept.Items.Clear();
            ddluser.Items.Clear();
            ddlDept.DataSource = ZLDC.DAL.Estate.DeptDAL.getGetAllChildrenDept("", "分行，区域");
            ddlDept.DataValueField = "id";
            ddlDept.DataTextField = "deptname";
            ddlDept.DataBind();
           
            ddlDept.Items.Insert(0, new ListItem("请选择"));
            ddluser.Items.Insert(0, new ListItem("请选择"));
        }
        //已删除部门用户
        protected void ddldelDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddldeluser.Items.Clear();
            ZLDC.DAL.Estate.DeptDAL.GetDelUserList(ddldeluser,ddldelDept.SelectedValue);
            ddldeluser.Items.Insert(0, new ListItem("请选择"));

        }

        protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddluser.Items.Clear();
            ZLDC.DAL.Estate.DeptDAL.GetUserList(ddluser, ddlDept.SelectedValue);
            ddluser.Items.Insert(0, new ListItem("请选择"));

        }

        protected void ddldeluser_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataSet ds = ZLDC.Topo.BLL.Estate.CustomerBLL.SelectHousingOrCustomerCount(ddldeluser.SelectedValue.ToString());
            lblHousing.Text = ds.Tables[0].Rows[0][0].ToString();
            lblCustomer.Text = ds.Tables[1].Rows[0][0].ToString();

        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            DataSet ds = ZLDC.Topo.BLL.Estate.CustomerBLL.SelectHousingOrCustomerCount(ddldeluser.SelectedValue.ToString());
            int hCount=_Convert.ToInt(ds.Tables[0].Rows[0][0].ToString());
            int cCount=_Convert.ToInt(ds.Tables[1].Rows[0][0].ToString());
            string deptname=ddlDept.SelectedItem.Text.ToString();//现在有部门名称
            ZLDC.Topo.Model.Estate.UserInfo user = ZLDC.Topo.BLL.Estate.UserBLL.GetUserInfoByUserNo(ddluser.SelectedValue.ToString());

            if (user == null) 
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('楼盘系统中没有此用户！')", true);
                return;
            }
            
            
            int uid = user.Id;//现在有部门人员ID
            deptname=deptname.Substring(1,deptname.Length-1);
            int rows = 0;
            if (hCount > 0 && cCount>0) 
            {
                //转盘
              rows= ZLDC.Topo.BLL.Estate.HousingBLL.UpdateStatus("0", _Convert.ToInt(ddldelDept.SelectedValue.ToString()), ddldelDept.SelectedItem.Text.ToString(), _Convert.ToInt(ddldeluser.SelectedValue.ToString()), ddldeluser.SelectedItem.Text.ToString(), "true",uid, _Convert.ToInt(ddlDept.SelectedValue.ToString()),deptname  , ddluser.SelectedItem.Text.ToString());
                //转客
              rows = ZLDC.Topo.BLL.Estate.CustomerBLL.UpdateStatus("0", _Convert.ToInt(ddldelDept.SelectedValue.ToString()), ddldelDept.SelectedItem.Text.ToString(), _Convert.ToInt(ddldeluser.SelectedValue.ToString()), ddldeluser.SelectedItem.Text.ToString(), "true", uid, _Convert.ToInt(ddlDept.SelectedValue.ToString()), deptname, ddluser.SelectedItem.Text.ToString());
             
              Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('操作成功！')", true);
             
            }
            if (cCount > 0 && hCount<=0)
            {
                rows = ZLDC.Topo.BLL.Estate.CustomerBLL.UpdateStatus("0", _Convert.ToInt(ddldelDept.SelectedValue.ToString()), ddldelDept.SelectedItem.Text.ToString(), _Convert.ToInt(ddldeluser.SelectedValue.ToString()), ddldeluser.SelectedItem.Text.ToString(), "true", uid, _Convert.ToInt(ddlDept.SelectedValue.ToString()), deptname, ddluser.SelectedItem.Text.ToString());
                Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('操作成功！')", true);
            }

            if (hCount > 0 && cCount<=0)
            {
               rows = ZLDC.Topo.BLL.Estate.HousingBLL.UpdateStatus("0", _Convert.ToInt(ddldelDept.SelectedValue.ToString()), ddldelDept.SelectedItem.Text.ToString(), _Convert.ToInt(ddldeluser.SelectedValue.ToString()), ddldeluser.SelectedItem.Text.ToString(), "true", uid, _Convert.ToInt(ddlDept.SelectedValue.ToString()), deptname, ddluser.SelectedItem.Text.ToString());
               Page.ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('操作成功！')", true);
            }
             NewMethod();
        }
    }
}
